import re
import env_set

def get_env_from_cache(log_file):
    with open(log_file, 'r') as file:
        for line in file:
            if "BOARD:STRING" in line:
                board = line.split("BOARD:STRING=")[1].strip()
            if "APPLICATION_SOURCE_DIR:PATH" in line:
                app_path = line.split("APPLICATION_SOURCE_DIR:PATH=")[1].strip()
            if "WEST_PYTHON:UNINITIALIZED=" in line:
                equal_pos = line.find('=')  # 等号的位置
                opt_pos = line.find('/opt')  # '/opt'的位置
                if equal_pos != -1 and opt_pos != -1:
                    toolchain_path = line[equal_pos + 1:opt_pos]
            if "NRF_DIR:PATH=" in line:
                equal_pos = line.find('=')
                opt_pos = line.find('/nrf')
                if equal_pos != -1 and opt_pos != -1:
                    sdk_path = line[equal_pos + 1:opt_pos]
    return board, app_path, toolchain_path, sdk_path

if __name__ == "__main__":
    log_file = 'CMakeCache.txt'
    board, app_path, toolchain_path, sdk_path = get_env_from_cache(log_file)
    env_set.generater_env_cmd(toolchain_path, sdk_path, app_path, board)